package ly.gupao.edu.java8.example3;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.*;

/**
 * @author: liyang
 * @date：2018/9/4 0004
 * describe：
 */
public class Example_3_Auto_Package {

    public static void main(String[] args) {
        IntPredicate evenNumbers = (int i) -> i % 2 == 0;
        DoublePredicate eventDouble = (double i) -> i % 2 == 0;
        LongPredicate eventLong = (long i) -> i % 2 == 0;
        Predicate<Integer> oddNumbers = (Integer i) -> i % 2 == 0;

        System.out.println(evenNumbers.test(3));
        System.out.println(oddNumbers.test(985));
        System.out.println(eventDouble.test(985.3));
        System.out.println(eventLong.test(985));

        IntConsumer intConsumer = (int i) -> System.out.println(i + 11);
        intConsumer.accept(11);
        DoubleConsumer doubleConsumer = (double d) -> System.out.println(d + 1.1);
        doubleConsumer.accept(22);

        IntToDoubleFunction intToDoubleFunction = (int i) -> i;
        System.out.println(intToDoubleFunction.applyAsDouble(3));

        int i = 3, j = 4;
        BooleanSupplier b = () -> i > j;
        System.out.println(b.getAsBoolean());

        DoubleSupplier doubleSupplier = () -> i;
        System.out.println(doubleSupplier.getAsDouble());

        IntUnaryOperator intUnaryOperator = (int k) -> k + 1;
        System.out.println(unary(intUnaryOperator, Arrays.asList(11, 13)));

        DoubleBinaryOperator doubleBinaryOperator = (double k, double l) -> k * l;
        System.out.println(doubleBinaryOperator.applyAsDouble(1, 99));

        BinaryOperator<Integer> binaryOperator = BinaryOperator.maxBy(Comparator.naturalOrder());
        System.out.println(binaryOperator.apply(1, 2));


        BiPredicate<Integer, Integer> biPredicate = (Integer k, Integer l) -> k > l;
        BiPredicate<Integer, Integer> biPredicate1 = (Integer k, Integer l) -> k > l;
        System.out.println(biPredicate.test(1, 2));
        System.out.println(biPredicate.test(3, 2));
        System.out.println(biPredicate.and(biPredicate1).test(3, 2));

    }

    public static Integer unary(IntUnaryOperator un, List<Integer> list){
        AtomicInteger k = new AtomicInteger();
        list.stream().forEach( e ->  k.addAndGet(un.applyAsInt(e)));
        return k.get();
    }

}
